<?php
/**
 * Created by WXCH
 * Date: 2020/1/10 0010 20:28
 */

namespace app\api\controller\v1;

use app\api\controller\ApiBase;
use app\api\service\store\Order as OrderService;
use app\api\service\user\User as UserService;

class Order  extends ApiBase {

    protected $sOrderService;

    public function __construct(OrderService $sOrderService){
        parent::__construct();
        $this->sOrderService = $sOrderService;
    }

    public function add(){
        //$orderInfo = input('post.orderInfo/a');
        $orderInfo = $this->request->post('orderInfo/a');
        $userId = UserService::getCurrentLoginUserId();

        $orderId = $this->sOrderService->add($orderInfo, $userId);
        return success('订单生成成功!', ["id" => $orderId]);
    }

    /**
     * 获取订单详情
     */
    public function getDetail(){
        $orderId = $this->request->param('id');
        $detail = $this->sOrderService->getDetail($orderId);
        return success('', compact('detail'));
    }

    /**
     * 获取未支付的订单详情(用于订单支付)
     */
    public function getUnpaidDetail(){
        $orderId = $this->request->param('id');
        $detail = $this->sOrderService->getUnpaidOrderDetail($orderId);
        return success('', compact('detail'));
    }

    /**
     * 全部订单（包括进行中、历史订单、退款订单）  进行中订单（商家已接单） 已完成订单（就是历史订单，设定时间自动完成）
     * @param int $status ['全部','进行中','已完成'],
     * @param int $page
     * @param int $size
     * @return array|\think\response\Json
     */
    public function getList($status = 0, $page = 1, $size = 30){
        /*$status = input('status', 0);
        $page = input('page', 1);
        $page = 15;*/

        $userId = UserService::getCurrentLoginUserId();

        return $this->sOrderService->getList($userId, $status, $page, $size);
    }


    /**
     * 获取当前用户待处理的订单数量
     */
    public function getTodoCounts()
    {
        $counts = $this->sOrderService->getTodoCounts();
        return success('查询成功', $counts);
    }


    /**
     * 确认收货
     */
    public function receipt(){
        $orderId = $this->request->param('id');
        $res = $this->sOrderService->receipt($orderId);
        if($res){
            return success('确认成功');
        }
        return error('确认失败');
    }


    public function toCancel(){
        $orderId = $this->request->param('id');
        $reason = $this->request->param('reason');
        $res = $this->sOrderService->toCancel($orderId, $reason);
        if($res){
            return success('取消成功',$res);
        }
        return error('取消失败');
    }

    public function toComment(){
        $commentInfo = $this->postData('commentInfo');
        $userId = UserService::getCurrentLoginUserId();

        $commentInfo['user_id'] = $userId;
        $res = $this->sOrderService->toComment($commentInfo);
        if($res){
            return success('评价成功');
        }
        return error('评价失败');
    }
















    public function getOrderReturnDetail(){
        $id = input('id', 0);
        $orderInfo = $this->sOrderService->getReturnOrderDetail($id);
        if(empty($orderInfo['orderreturn']['return_img'])){
            $orderInfo['orderreturn']['return_img_arr'] = [];
        }else{
            $return_img_arr = explode(',', $orderInfo['orderreturn']['return_img']);
            foreach ($return_img_arr as $k => $v){
                $return_img_arr[$k] = config("setting.domain_prefix").config("setting.return_prefix").$v;
            }
            $orderInfo['orderreturn']['return_img_arr'] = $return_img_arr;
        }

        return Show::success('查询成功', ['orderInfo' => $orderInfo]);
    }

    public function getOrderReturnList($status = 0, $page = 1, $size = 30){
        /*$status = input('status', 0);
        $page = input('page', 1);
        $page = 15;*/
        return $this->sOrderService->getOrderReturnList($this->user_id,$status, $page, $size);
    }



    public function orderRefund(){
        $refundInfo = input('post.refundInfo/a');
        $refundInfo['user_id'] = $this->user_id;
        $res = $this->sOrderService->orderRefund($refundInfo);
        
        if($res){

            return Show::success('提交成功');
        }
        return Show::error('提交失败');
    }

    public function getOrderExpress(){
        $id = input('get.id', 0);//订单id
        $res = $this->sOrderService->getOrderExpress($id);
        if($res){
            return Show::success('获取物流信息成功', $res);
        }
        return Show::error('获取物流信息失败');
    }

}